﻿using UnityEngine;
using System.Collections;
using CinemaDirector;

class GameLayer
{
    public const int Skill = 8;
}

	/// <summary>
	/// A custom event that will set a GameObject's specific component enable state at a given time. 
	/// </summary>
    //[CutsceneItemAttribute("CoolGame", "PlayEffect", CutsceneItemGenre.GlobalItem)]
	//[USequencerEventHideDuration()]
public class CGPlayEffect : CinemaGlobalAction
	{
        public string effectRes;
        public string followPosition = "";
        public Vector3 position;
        public Vector3 Rotation = Vector3.zero;
        public int layer = GameLayer.Skill;

        static string effectResPath = "Effect";
        GameObject effect;
        Transform followTransform;

        public override void Trigger()
		{
            string resPath = effectResPath + "/" + effectRes + "/" + effectRes;
            GameObject res = U3DResourcesMgr.LoadRes(resPath) as GameObject;
            if (res == null)
            {
                return;
            }
            effect = GameObject.Instantiate<GameObject>(res);

            if (followPosition != "")
            {
                var tmp = U3DHelp.FindChildInGameObject(followPosition);
                if (tmp == null)
                {
                    Debug.Log("Can not find follow position object: " + followPosition);
                    return;
                }
                followTransform = tmp.transform;
                effect.transform.position = followTransform.position;
            }
            else
            {
                effect.transform.localPosition = position;
            }
            
            effect.transform.localRotation = Quaternion.Euler(Rotation);
            
            U3DHelp.ChangeGameObjectLayer(effect, layer);
            effect.SetActive(true);
		}

        public override void UpdateTime(float time, float deltaTime)
        {
            if (followTransform != null)
                effect.transform.position = followTransform.position;
        }

        public override void End()
        {
            if (effect != null)
            {
                GameObject.DestroyImmediate(effect);
                effect = null;
                followTransform = null;
            }
                
        }

        public override void Stop()
        {
            End();
        }
	}